﻿Imports TBCC.UtilityMobile
Public Class ConnectForm

    Private _pi As ProjectItem
    Private _oldConnectType As Integer
    Private _ConnectType As Integer
    Private _ComNumber As Integer
    Private _Phone As String

    Private Sub ConnectForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For Each com As String In System.IO.Ports.SerialPort.GetPortNames()
            Me.cboCOM.Items.Add(com)
        Next

        LoadData()

    End Sub

    Private Sub LoadData()

        Try


            If KeyValuePairClass.GetValue1("ConnectType") = "" Then
                KeyValuePairClass.SetValue1("ConnectType", "1")
            End If
            If KeyValuePairClass.GetValue1("ComNumber") = "" Then
                KeyValuePairClass.SetValue1("ComNumber", "6")
            End If

            _ConnectType = KeyValuePairClass.GetValue1("ConnectType")
            _ComNumber = KeyValuePairClass.GetValue1("ComNumber")
            _Phone = KeyValuePairClass.GetValue1("Phone")
            _oldConnectType = _ConnectType

            Select Case _ConnectType
                Case 0 '拨号
                    Me.optDial.Checked = True
                    Me.cboCOM.SelectedItem = "COM" & _ComNumber
                    Me.txtPhone.Text = _Phone

                Case 1 '直连
                    Me.optCOM.Checked = True
                    Me.cboCOM.SelectedItem = "COM" & _ComNumber
                    Me.txtIPAddress.Text = _Phone

                Case 2 'TCP
                    Me.optTcp.Checked = True
                    Me.txtIPAddress.Text = _Phone
                    Me.nudIPPort.Value = _ComNumber

            End Select

        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try


    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Me.btnOk.Enabled = False

        If CheckValid() = True Then
            If Save() = True Then
                If Connect() = True Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK

                    Me.Close()
                End If


            End If
        End If

        Me.btnOk.Enabled = True
    End Sub

    Private Function Save() As Boolean
        Try
            If Me.optCOM.Checked = True Then
                _ConnectType = 1
                _ComNumber = Me.cboCOM.SelectedItem.Substring(3)
                _Phone = Me.txtPhone.Text

            ElseIf Me.optDial.Checked = True Then
                _ConnectType = 0
                _ComNumber = Me.cboCOM.SelectedItem.Substring(3)
                _Phone = Me.txtPhone.Text
               

            ElseIf Me.optTcp.Checked = True Then
                _ConnectType = 2
                _ComNumber = CInt(Me.nudIPPort.Value)
                _Phone = Me.txtIPAddress.Text

            End If

            KeyValuePairClass.SetValue("0", "ConnectType", _ConnectType)
            KeyValuePairClass.SetValue("0", "ComNumber", _ComNumber)
            KeyValuePairClass.SetValue("0", "Phone", _Phone)

        Catch ex As Exception
            'msg.ShowError("")
            Return False
        End Try

        Return True



    End Function

    Private Function CheckValid() As Boolean
        '检查连接方式
        If Me.optCOM.Checked = False AndAlso Me.optDial.Checked = False AndAlso Me.optTcp.Checked = False Then
            msg.Show("请选择连接方式。")
            Me.optCOM.Focus()
            Return False
        End If

        '检查串口号
        If Me.optCOM.Checked = True OrElse Me.optDial.Checked = True Then
            If Me.cboCOM.SelectedIndex < 0 Then
                msg.Show("请选择串口号。")
                Me.cboCOM.Focus()
                Return False
            End If
        End If

        '检查电话号码
        If Me.optDial.Checked = True Then
            If Me.txtPhone.Text.Trim.Length <= 0 Then
                msg.Show("请输入电话号码。")
                Me.txtPhone.Focus()
                Return False
            End If
        End If

        '检查TCP参数
        If Me.optTcp.Checked = True Then
            '检查IP地址
            If Me.txtIPAddress.Text.Trim.Length <= 0 Then
                msg.Show("请输入IP地址。")
                Me.txtIPAddress.Focus()
                Return False
            End If
            '检查IP端口
            If Me.nudIPPort.Value <= 0 Then
                msg.Show("IP端口不能为零。")
                Me.nudIPPort.Focus()
                Return False
            End If
        End If

        Return True
    End Function

#Region " 连接函数 "

    Private Function Connect() As Boolean
        Return Connect(False)
    End Function
    Private Function Connect(ByVal isRetry As Boolean) As Boolean

        COMHelper.ConnectType = _ConnectType

        If _ConnectType = 1 Then
            '直连 
            'Me.Cursor = Cursors.WaitCursor
            If InitCOM() = False Then
                Return False
            End If
            'Me.Cursor = Cursors.Default
        ElseIf _ConnectType = 0 Then
            '拨号
            Dim frm As New DialForm("COM" & _ComNumber, _Phone)
            If frm.ShowDialog <> Windows.Forms.DialogResult.OK Then
                Return False
            End If
        Else
            'TCP/IP
            Dim frm As New TcpConnectForm(isRetry, _Phone, _ComNumber)
            If frm.ShowDialog() <> Windows.Forms.DialogResult.OK Then
                Return False
            End If
        End If

        Return True
    End Function

#Region " 直连方式 "

    Private Function InitCOM() As Boolean
        Dim com As String
        com = _ComNumber.ToString()

        If com = "" Then
            msg.Show("请先配置串口")
            Return False
        End If

        If COMHelper.Instance.InitSerialPort(com.Replace("COM", ""), IO.Ports.Parity.Even) Then
            Return DirectConnect()
        Else
            msg.ShowError("初始化串口失败,请检查串口配置")
            Return False
        End If
    End Function

    'Private Function DirectConnect() As Boolean
    '    Dim strResult As String

    '    For i As Integer = 0 To 2

    '        strResult = COMHelper.Instance.WriteCommand(UtilityClass.GetAuthCodeByte(_pi.AuthCode), 10, 10000)
    '        If strResult.IndexOf("AT^AUTH=OK") >= 0 Then
    '            Return True
    '        Else
    '            msg.ShowError("与下位机的连接鉴权失败。")
    '            Return False
    '        End If

    '        System.Threading.Thread.Sleep(1000)
    '        Application.DoEvents()
    '    Next

    '    msg.ShowError("无法建立于下位机的连接")

    '    Return False
    'End Function
    Private Function DirectConnect() As Boolean
        Dim buf As Short() = Nothing
        Dim pacID As String = Nothing
        Dim devType As Integer = Nothing
        Dim writeState As Integer = Nothing

        Return True


        'FailedHandler:
        '        msg.ShowError("未检测到设备连接。")
        '        Return False

    End Function


#End Region

#End Region

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub optDial_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDial.CheckedChanged
        Me.pnlTCP.Enabled = False
        Me.pnlTCP.Visible = False
        Me.pnlCom.Enabled = True
        Me.pnlCom.Visible = True
        Me.pnlPhone.Enabled = True
        Me.pnlPhone.Visible = True
    End Sub

    Private Sub optCOM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCOM.CheckedChanged
        Me.pnlTCP.Enabled = False
        Me.pnlTCP.Visible = False
        Me.pnlPhone.Enabled = False
        Me.pnlPhone.Visible = False
        Me.pnlCom.Enabled = True
        Me.pnlCom.Visible = True
    End Sub

    Private Sub optTcp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optTcp.CheckedChanged
        Me.pnlCom.Enabled = False
        Me.pnlCom.Visible = False
        Me.pnlPhone.Enabled = False
        Me.pnlPhone.Visible = False
        Me.pnlTCP.Enabled = True
        Me.pnlTCP.Visible = True
    End Sub

    Private Sub btnConnectConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectConfig.Click
        Dim parameters(2) As String

        parameters(0) = KeyValuePairClass.GetValue1("ClientSocket_Heartbeat_IdleTime")
        parameters(1) = KeyValuePairClass.GetValue1("ClientSocket_Heartbeat_WaitTime")
        parameters(2) = KeyValuePairClass.GetValue1("ClientSocket_Heartbeat_Rep")

        Dim frm As New ConnectConfigForm(parameters)
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            KeyValuePairClass.SetValue1("ClientSocket_Heartbeat_IdleTime", parameters(0))
            KeyValuePairClass.SetValue1("ClientSocket_Heartbeat_WaitTime", parameters(1))
            KeyValuePairClass.SetValue1("ClientSocket_Heartbeat_Rep", parameters(2))
        End If
    End Sub

End Class